
# -*- coding: utf-8 -*-
import requests
import json

from config import BaseConfig


def push_report(web_hook,title,stock_name,open,high,low,now,support_price,pressure_price):
    # 定时任务触发钉钉报告推送

    header = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    message_body = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": "#### 股票：%s \n" % stock_name +
                    "##### •  开盘：%s \n" % open +
                    "##### •  最高：%s \n" % high +
                    "##### •  最低：%s \n" % low +
                    "##### •  现价：%s \n" % now +
                    "##### •  支撑位：%s \n" % support_price +
                    "##### •  阻力位：%s \n" % pressure_price
        },
        "at": {
            "atMobiles": [],
            "isAtAll": True
        }
    }
    send_data = json.dumps(message_body)  # 将字典类型数据转化为json格式
    ChatBot = requests.post(url=web_hook, data=send_data, headers=header)
    opener = ChatBot.json()
    if opener["errmsg"] == "ok":
        print(u"%s 通知消息发送成功！" % opener)
    else:
        print(u"通知消息发送失败，原因：{}".format(opener))


if __name__ == '__main__':
    # webhook 来自于 获取机器人webhook：复制webhook 中的那个值
    webhook = BaseConfig.DINGTALK_WEBHOOK
    push_report(webhook,'买卖通知','格力',10,10,9,9.03,8,11)